package cn.zj.admin.modules.issues.apis;

import cn.zj.admin.modules.issues.bean.RoleSearch;
import cn.zj.admin.modules.issues.bean.TableData;
import cn.zj.admin.modules.issues.domain.Role;
import cn.zj.admin.modules.issues.repository.RoleRepository;
import com.zj.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xi.yang
 * @create 2020-10-23 12:27
 **/
@RestController
@RequestMapping("system/role")
public class RoleApi {
    @Autowired
    private RoleRepository roleRepository;
    @GetMapping("list")
    public String init(RoleSearch roleSearch) {
        Page<Role> pageData = roleRepository.findAll(PageRequest.of(roleSearch.getPage() - 1, roleSearch.getLimit()));
        return JsonUtil.toString(TableData.builder()
                .count(pageData.getTotalElements())
                .data(pageData.getContent())
                .build());
    }

    @PostMapping
    public String save(@RequestBody Role role) {
        role.setCreateTime(System.currentTimeMillis());
        roleRepository.save(role);
        return JsonUtil.toString(role);
    }

    @DeleteMapping
    public boolean delete(@RequestBody List<Role> roles) {
        roleRepository.deleteAll(roles);
        return true;
    }

}
